package com.xxgc.springboot.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @program: springboot
 * @description:
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-09-08 11:12
 **/
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 时间日期格式化
     */
    @Value("${spring.jackson.date-format}")
    private String pattern;
    /**
     * 时间出参序列化
     * 使用此方法, 以下 spring-boot: jackson时间格式化 配置 将会失效
     * spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
     * spring.jackson.time-zone=GMT+8
     * 原因: 会覆盖 @EnableAutoConfiguration 关于 WebMvcAutoConfiguration 的配置
     * 如有特定返回格式配置 JsonFormat 来实现
     * */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = converter.getObjectMapper();
        // 时间格式化
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        System.out.println("pattern = " + pattern);
        objectMapper.setDateFormat(new SimpleDateFormat(pattern));
        // 设置格式化内容
        converter.setObjectMapper(objectMapper);
        converters.add(0, converter);
    }


    //在此处，将拦截器注册为一个 Bean
    //解决在MyInterceptor里面@Autowired不生效问题
    @Bean
    public MyInterceptor getMyInterceptor() {
        return new MyInterceptor();
    }
    /**
     * 拦截器需要拦截的接口
     */
/*    public void addInterceptors(InterceptorRegistry registry) {
        //----------------哪些需要拦截-----------------
        ArrayList<String> pathPatterns = Lists.newArrayList();
        pathPatterns.add("/**");//需要拦截哪些接口
        registry.addInterceptor(getMyInterceptor())//通过自定义拦截器进行拦截
                .addPathPatterns(pathPatterns)
                .excludePathPatterns("/swagger-resources/**", "/webjars/**",
                "/v2/**", "/swagger-ui.html/**","/doc.html/**");
        //不拦截哪些接口
        super.addInterceptors(registry);
    }*/

    /**
     * 解决swagger-ui.html 404无法访问的问题
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //-------------不需要拦截的部分------------------
        // 解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}
